(function(){
   var page = 1;	
   var hotType = "";
   var asideLock = true;
   var classifyId = document.location.search ? document.location.search.slice(1).split('=')[1] : "";
   //获取内容区域数据以及侧边栏
   function courseListContent(fn){
	    $.ajax({
	    	 url : '../../portal/courselibrary/findCourseLibrary.json?queryClassifyId=' + classifyId + '&currentPage=' + page + '&hotNews=' + hotType,
	    	 type : 'get',
	    	 dataType : 'json',
	    	 success : function(result){
	    		 if(result.success){
	    		   renderDom(result);
	    		   renderAside(result);
	    		   //找到最大页码数
	    		   var maxPage = result.data.courseQuery.maxPage || 1;
	    		   if(fn){fn(maxPage)};
	    		 }
	    	 }
	    })
   }
   //一上来先调用这个方法
   courseListContent(pageComponents);
   crumbsNav();
   //最新最热的标签事件
   $('#newEst').click(function(){
	   if(hotType == 1){return};
	   hotType = 1;
	   page = 1;
	   courseListContent(pageComponents);
	   $(this).addClass('active').siblings().removeClass('active');
	   $('#curricula').removeClass('active');
	   console.log($('#curricula'))
   })
   $("#hotTest").click(function(){
	   if(hotType == 2){return};
	   hotType = 2;
	   page = 1;
	   courseListContent(pageComponents);
	   $(this).addClass('active').siblings().removeClass('active');
	   $('#curricula').removeClass('active');
   })
   //全部课程标签
    $('.wrapper').delegate('.allCourse','click',function(){
    	if($(this).hasClass('Taxonomy'))return;
    	page = 1;
    	hotType = '';
    	classifyId = '';
    	courseListContent(pageComponents);
    	$('.center-title li').removeClass('active');
    	$(this).parent('#curricula').addClass('active');
    	$('#curricula .Taxonomy').text('全部课程').removeClass('Taxonomy').addClass('allCourse');
    	crumbsNav();
    })
    
    //分类课程标签
    $('#curricula').delegate('.Taxonomy','click',function(){
    	   if($(this).hasClass('allCourse'))return;
    	   classifyId = $(this).attr('classifyId');
    	   $(this).parent('#curricula').addClass('active');
		   page = 1;
		   hotType = '';
		   courseListContent(pageComponents);
		   $('.center-title li').removeClass('active');
    })
  //将渲染dom提出长一个函数
   function renderDom(result){
	    //遍历输出正确时间
	   for(var i = 0; i < result.data.courseList.length; i++){
	     var time = new Date(result.data.courseList[i].modifyTime);
	     var month = (time.getMonth() + 1) > 9 ? (time.getMonth() + 1) : '0' + (time.getMonth() + 1);
	     var day = time.getDate();
	     result.data.courseList[i].time = month + '/' + day;
	     }
	    var contentDOM = _.template($('#courseListContentTpl').html())(result.data);
	    $('#course-list-section').html(contentDOM);
   }
   //侧边栏点击事件
	   $('.bd').delegate('.Taxonomy','click',function(){
		   classifyId = $(this).attr('classifyId');
		   page = 1;
		   hotType = '';
		   $('#curricula').addClass('active');
		   $('.center-title li').removeClass('active');
		   courseListContent(pageComponents);
		   crumbsNav(classifyId);
	   })
   //课程库面包屑导航
   function crumbsNav(id){
	   if(!id){
		var crumbsNavDOM = _.template($('#crumbsNav').html())({data:[]});
		$('#breadChippingBox').html(crumbsNavDOM);
		return;
	   }
	   var courseClassify = $('#allCourse');
	   $.ajax({
		   url:'../../portal/courselibrary/findClassifyNav.json',
		   type:'GET',
		   data:{
			  classifyId : id
		   },
		   dataType:'json',
		   success:function(result){
			 if(result.success){
				var crumbsNavDOM = _.template($('#crumbsNav').html())(result);
				$('#breadChippingBox').html(crumbsNavDOM)
				if(result.data.length >= 1){
					var classIfyObj = result.data[result.data.length-1];
					courseClassify.text(classIfyObj.classifyName);
					courseClassify.addClass('Taxonomy').attr('classifyid',classIfyObj.classifyId).removeClass('allCourse');
				}
				else {
					
				}
			 }
		   }
	   })	   
	}
   //面包屑导航点击事件
   $('#breadChippingBox').delegate('a.Taxonomy','click',function(){
	       classifyId = $(this).attr('classifyId');
		   page = 1;
		   hotType = '';
		   $('#curricula').addClass('active');
		   $('.center-title li').removeClass('active');
		   courseListContent(pageComponents);
		   crumbsNav(classifyId)
   })
   //找到选中的分类高亮显示
   function showClassIfy(result){
	   $('.bd .Taxonomy').removeClass('active');
	   $.each(result,function(index,val){
		   $.each($('.bd .Taxonomy'),function(){
			   if($(this).attr('classifyid') == val.classifyId){
				   $(this).addClass('active');
			   }
		   })
	   })
   }
   //课程详情页面返回的时候，我们需要解析hash来渲染面包屑导航
   classifyId && crumbsNav(classifyId);
   //将翻页组件封装一下，以便于方便调用
   function pageComponents(maxPage){
		  var maxPage = maxPage;
		  //将翻页组件插入进来
		  var components = new PageNum(maxPage);
		  //为翻页添加事件；
		  components.events(function(num){
			      page = num;
			      courseListContent();
		  })
	}
   //渲染侧边栏
   function renderAside(result){
	   for(var i = 0 ; i < result.data.classifyList.length; i++){
		   if(result.data.classifyList[i].classifyName.length<=6){
			   result.data.classifyList[i].classifyNameNew = result.data.classifyList[i].classifyName;
		   }else{
			   result.data.classifyList[i].classifyNameNew = result.data.classifyList[i].classifyName.substring(0,6)+"...";
		   }
	   }
	   var asideDOM = _.template($('#courseSliderTpl').html())(result.data)
	   $('.bd').html(asideDOM); 
   }
})()